package com.wxm.common.file.core.service.supplier;

import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectApiBuilder;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.auth.ObjectAuthorization;
import cn.ucloud.ufile.auth.UfileObjectLocalAuthorization;
import com.wxm.common.file.core.enm.FileMimeEnum;
import com.wxm.common.file.core.exception.FileException;
import com.wxm.common.file.core.property.UFileUs3Property;
import com.wxm.common.file.core.property.UploadBaseProperty;

import java.io.InputStream;

/**
 * <p>
 *      供应商[UFileUs3文件储存]Service
 * </p>
 *
 * @author: wxm
 * @Date: 2022/1/22
 */
public class UFileUs3Service implements SupplierBaseService{
    private UFileUs3Property property;
    private ObjectConfig config;
    private ObjectAuthorization objectAuthorization;
    private ObjectApiBuilder objectApiBuilder;

    /**
     * 参数非空校验
     */
    private void checkProperty(){
        FileException.isNull(this.property.getPublicKey(),"PublicKey不能为null");
        FileException.isNull(this.property.getPrivateKey(),"PrivateKey不能为null");
        FileException.isNull(this.property.getBucketRegion(),"BucketRegion不能为null");
        FileException.isNull(this.property.getProxySuffix(),"ProxySuffix不能为null");
        FileException.isNull(this.property.getBucket(),"Bucket不能为null");
        FileException.isNull(this.property.getRootPath(),"RootPath不能为null");
    }

    @Override
    public void init(UploadBaseProperty uploadBaseProperty) {
        this.property = (UFileUs3Property)uploadBaseProperty; // 上传参数
        this.checkProperty();
        this.config = new ObjectConfig(this.property.getBucketRegion(),this.property.getProxySuffix()); // 对象操作需要ObjectConfig来配置您的地区和域名后缀
        this.objectAuthorization  = new UfileObjectLocalAuthorization(this.property.getPublicKey(), this.property.getPrivateKey()); // 对象相关API的授权器
        this.objectApiBuilder = UfileClient.object(this.objectAuthorization, this.config);
    }

//    @Override
//    public void uploading(String filePath) throws Exception {
//        this.uploading(new URL(filePath).openStream(),filePath.substring(filePath.lastIndexOf("/")));
//    }
//
//    @Override
//    public void uploading(byte[] uploadBytes, String name) throws Exception {
//        this.uploading(new ByteArrayInputStream(uploadBytes),name);
//    }
//
//    @Override
//    public void uploading(File file) throws Exception {
//        this.objectApiBuilder.putObject(file, FileMimeEnum.getMimeByType(file.getName().substring(file.getName().lastIndexOf("."))))
//                .nameAs(file.getName())
//                .toBucket(this.property.getBucket())
//                .execute();
//    }

    @Override
    public void uploading(InputStream inputStream, String fileName) throws Exception {
        this.objectApiBuilder.putObject(inputStream, FileMimeEnum.getMimeByType(fileName.substring(fileName.lastIndexOf("."))))
                .nameAs(fileName)
                .toBucket(this.property.getBucket())
                .execute();
    }

    @Override
    public void delete(String filePath) throws Exception {
        //UFile需要去掉前缀
        String deletePath = this.removeFilePathPrefix(filePath, this.property.getRootPath());
        this.objectApiBuilder.deleteObject(deletePath,this.property.getBucket()).execute();
    }
}
